Trò chơi đua xe động vật trong UNITY Engine
114.680 lượt xem;
- GameScreen.cs
- GameScreen /
- AnimalRacing /
- Scripts /
- Assets /
- project /
1 using UnityEngine;
2 using System.Collections.Generic;
3 using Dialog;
4
5 namespace GamePlay
6 {
7 public class GameScreen : MonoBehaviour
8 {
9 private GameObject mapObject;
10 public Animals animals;
11 public GameObject goldLayer;
12 public GameObject coinLayer;
13 public GameObject boosterLayer;
14 public GameObject springsLayer;
15 public GameObject skillRandomLayer;
16 public GameObject shadowLayer;
17 public TaskBar taskbar;
18 public ButtonSkills buttonSkills;
19
20 public GameObject pauseLayer;
21
22 public Background background;
23
24 public GameObject bulletLayer;
25
26 public GameObject effectLayer;
27
28 public GameObject scoreLayer;
29
30 private GameObject deadLine;
31
32 public RevivalTask revivalTask;
33
34 private List<Vector3> revivalPositions;
35
36 public float m_time { get; set; }
37 public int m_gold { get; set; }
38 public int m_score { get; set; }
39
40 private bool isRunning;
41 private bool isPrepare;
42
43 private GameObject guideLayer;
44 public GameObject counterLayer;
45
46 public GameObject resultLayer;
47
48 private int[] skills;
49 private bool useSkills;
50
51 private BitmapFont shopFont;
52
53 private int[] perSkills;
54
55 public Combo combo1;
56 public Combo combo2;
57
58 public GameObject dialog;
59
60 public GoldAddition goldAddition;
61
62 public void Start()
63 {
64 InputController.Name = InputNames.DIALOG;
65
66 string[] mapNames = new string[] {"jungle","southpole","desert","volcano"};
67 int[] lvs = new int[] { 1, 2, 3, 2, 3, 4, 3, 4, 5, 1, 2, 4, 3, 4, 5 };
68
69 Debug.Log("WorldIndex : " + Attr.currentWorld + ", LevelIndex : " + Attr.currentLevel);
70
71 mapObject = (GameObject)Instantiate(Resources.Load("Maps/" + mapNames[Attr.currentWorld] + lvs[Attr.currentLevel]), new Vector3(-4, 2.5f, 0), Quaternion.identity);
72 mapObject.transform.localScale = new Vector3(0.01f, 0.01f, mapObject.transform.localScale.z);
73
74 revivalPositions = new List<Vector3>();
75
76 getSkillChoosed();
77
78 setupMap(mapObject);
79
80 int mapLayer = LayerMask.NameToLayer("Map");
81 mapObject.layer = mapLayer;
82
83 Transform[] transforms = mapObject.GetComponentsInChildren<Transform>(true);
84 for (int i = 0; i < transforms.Length; i++)
85 {
86 transforms[i].gameObject.layer = mapLayer;
87 }
88
89 shopFont = new BitmapFont("Fonts/shop_font", "Fonts/shop_font_xml", null);
90
91 BitmapFont comboFont = new BitmapFont("Fonts/font_combo", "Fonts/font_combo_xml", null);
92 combo1.setFont(comboFont);
93 combo2.setFont(comboFont);
94
95 createDialog();
96
97 m_gold = 0;
98 m_time = 0;
99 m_score = 0;
100
101 perSkills = new int[] { 30, 30, 35, 35, 40, 40, 45, 45, 50, 55, 60, 60, 65, 65, 70 };
102
103 isRunning = true;
104 //prepareGame();//khong goi o day vi co the chua chay het cac ham Start
105 isPrepare = true;
106 }
107
108 private void getSkillChoosed()
109 {
110 List<int> sks = new List<int>();
111 for (int i = 0; i < 3; i++)
112 {
113 if (Attr.currentSkills[i] != -1)
114 {
115 sks.Add(Attr.currentSkills[i]);
116 }
117 }
118 if (sks.Count > 0)//neu co chon skill
119 {
120 useSkills = true;
121 skills = new int[sks.Count];
122 for (int i = 0; i < sks.Count; i++)
123 {
124 skills[i] = sks[i];
125 }
126 }
127 else//neu khong chon skill thi lay tu cac skill unlock
128 {
129 int[] unlockStars = new int[] { 0, 0, 35, 135, 15, 55, 75, 95 };
130
131 int totalStar = 0;
132 for (int i = 0; i < 60; i++)
133 totalStar += Data.getData(Data.KEY_STAR + i);
134
135 for (int i = 0; i < 8; i++)
136 {
137 if (totalStar >= unlockStars[i])
138 {
139 sks.Add(i);
140 }
141 }
142 skills = new int[sks.Count];
143 for (int i = 0; i < sks.Count; i++)
144 {
145 skills[i] = sks[i];
146 }
147 }
148 }
149
150 private void setupMap(GameObject mObject)
151 {
152 GameObject markGounds = new GameObject("MarkGrounds");
153 markGounds.transform.parent = mObject.transform;
154 markGounds.transform.localScale = new Vector3(1, 1, 1);
155 markGounds.transform.localPosition = new Vector3(0, 0, 0);
156
157 for (int i = 0; i < mObject.transform.childCount; i++)
158 {
159 Transform childTransform = mObject.transform.GetChild(i);
160 string childname = childTransform.gameObject.name;
161 if (childname == MapObjectNames.MarkObject)
162 {
163 createMarkObjects(childTransform);
164 }
165 else if (childname == MapObjectNames.BoostObject)
166 {
167 createBoosterObjects(childTransform);
168 }
169 else if (childname == MapObjectNames.SpringsObject)
170 {
171 createSpringsObjects(childTransform);
172 }
173 else if (childname == MapObjectNames.HoleObject)
174 {
175 createHoles(childTransform);
176 }
177 else if (childname == MapObjectNames.Coin)
178 {
179 createCoins(childTransform);
180 }
181 else if (childname == MapObjectNames.SkillObject)
182 {
183 createSkillRandoms(childTransform);
184 }
185 else if (childname == MapObjectNames.RevivalObject)
186 {
187 createRevivalObjects(childTransform);
188 }
189 else if (childname == MapObjectNames.GroundObject)
190 {
191 createGrounds(childTransform, markGounds.transform);
192 }
193 else if (childname == MapObjectNames.FinishObject)
194 {
195 createFinishObject(childTransform);
196 }
197 }
198
199 createDeadLine();
200 }
201
202 private void createMarkObjects(Transform parentTransform)
203 {
204 for (int i = 0; i < parentTransform.childCount; i++)
205 {
206 GameObject markObject = parentTransform.GetChild(i).gameObject;
207 markObject.name = MapObjectNames.MarkObject;
208 if (markObject.GetComponent<BoxCollider2D>() != null)
209 Destroy(markObject.GetComponent<BoxCollider2D>());
210 BoxCollider2D childCollider = markObject.AddComponent<BoxCollider2D>();
211 childCollider.size = new Vector2(20, 1000);
212 childCollider.isTrigger = true;
213 }
214 }
215
216 private void createBoosterObjects(Transform parentTransform)
217 {
218 for (int i = 0; i < parentTransform.childCount; i++)
219 {
220 GameObject boosterObject = (GameObject)Instantiate(Resources.Load<GameObject>("Booster"));
221 GameObject c = parentTransform.GetChild(i).gameObject;
222 boosterObject.transform.position = c.transform.position;
223 boosterObject.transform.localScale = new Vector3(1, 1, 0);
224 boosterObject.transform.parent = boosterLayer.transform;
225 BoxCollider2D childCollider = boosterObject.AddComponent<BoxCollider2D>();
226 childCollider.size = new Vector2(0.9f, 0.2f);
227 childCollider.isTrigger = true;
228 boosterObject.transform.position -= new Vector3(-0.45f, 0.1f, 0);
229 boosterObject.name = MapObjectNames.BoostObject;
230
231 Destroy(c);
232 }
233 }
234
235 private void createSpringsObjects(Transform parentTransform)
236 {
237 for (int i = 0; i < parentTransform.childCount; i++)
238 {
239 GameObject springsObject = (GameObject)Instantiate(Resources.Load<GameObject>("Springs"));
240 GameObject c = parentTransform.GetChild(i).gameObject;
241 springsObject.transform.position = c.transform.position;
242 springsObject.transform.parent = springsLayer.transform;
243 springsObject.transform.position -= new Vector3(-0.2f, 0.3f, 0);
244 springsObject.name = MapObjectNames.SpringsObject;
245 Destroy(c);
246 }
247 }
248
249 private void createHoles(Transform parentTransform)
250 {
251 for (int i = 0; i < parentTransform.childCount; i++)
252 {
253 BoxCollider2D childCollider = parentTransform.GetChild(i).gameObject.GetComponent<BoxCollider2D>();
254 childCollider.isTrigger = true;
255 parentTransform.GetChild(i).gameObject.name = MapObjectNames.HoleObject;
256 }
257 }
258
259 private void createCoins(Transform parentTransform)
260 {
261 for (int i = 0; i < parentTransform.childCount; i++)
262 {
263 GameObject coinObject = (GameObject)Instantiate(Resources.Load<GameObject>("Coin"));
264 coinObject.name = MapObjectNames.Coin;
265 GameObject c = parentTransform.GetChild(i).gameObject;
266 coinObject.transform.position = c.transform.position;
267 coinObject.transform.localScale = new Vector3(1, 1, 0);
268 coinObject.transform.parent = coinLayer.transform;
269 BoxCollider2D childCollider = coinObject.AddComponent<BoxCollider2D>();
270 childCollider.size = new Vector2(0.3f, 0.3f);
271 childCollider.isTrigger = true;
272
273 Destroy(c);
274 }
275 }
276
277 private void createSkillRandoms(Transform parentTransform)
278 {
279 for (int i = 0; i < parentTransform.childCount; i++)
280 {
281 GameObject skillObject = (GameObject)Instantiate(Resources.Load<GameObject>("SkillRandom"));
282 skillObject.name = MapObjectNames.SkillObject;
283 GameObject c = parentTransform.GetChild(i).gameObject;
284 skillObject.transform.position = c.transform.position;
285 skillObject.transform.parent = skillRandomLayer.transform;
286 skillObject.transform.position -= new Vector3(-0.2f, 0.2f, 0);
287
288 skillObject.AddComponent<SkillRandom>().skillType = SkillRandom.getSkillType(skills[Random.Range(0, skills.Length)]);
289 // Debug.Log(skillObject.GetComponent<SkillRandom>().skillType.ToString());
290
291 Destroy(c);
292 }
293 }
294
295 private void createRevivalObjects(Transform parentTransform)
296 {
297 for (int i = 0; i < parentTransform.childCount; i++)
298 {
299 GameObject revivalObject = parentTransform.GetChild(i).gameObject;
300 if (revivalObject.GetComponent<BoxCollider2D>() != null)
301 Destroy(revivalObject.GetComponent<BoxCollider2D>());
302 BoxCollider2D childCollider = revivalObject.AddComponent<BoxCollider2D>();
303 childCollider.size = new Vector2(20, 1000);
304 childCollider.isTrigger = true;
305 revivalObject.name = MapObjectNames.RevivalObject;
306 revivalPositions.Add(revivalObject.transform.localPosition);
307 revivalObject.AddComponent<RevivalObject>().revialIndex = i;
308 }
309 }
310
311 public Vector3 getRevivalPosition(int revivalIndex)
312 {
313 if(revivalIndex >= revivalPositions.Count)
314 return revivalPositions[revivalPositions.Count - 1];
315 return revivalPositions[revivalIndex];
316 }
317
318 private void createGrounds(Transform parentTransform, Transform markGounds)
319 {
320 for (int i = 0; i < parentTransform.childCount; i++)
321 {
322 GameObject groundObject = parentTransform.GetChild(i).gameObject;
323 groundObject.name = "GroundObject";
324 Rigidbody2D groundBody = groundObject.AddComponent<Rigidbody2D>();
325 groundBody.isKinematic = true;
326 groundBody.gravityScale = 0;
327
328 BoxCollider2D groundCollider = groundObject.GetComponent<BoxCollider2D>();
329
330 //them 1 cai gameobject chua boxcollider ngay truoc cac khoi dat
331 float height = groundObject.GetComponent<BoxCollider2D>().size.y;
332 GameObject markObject = new GameObject("MarkGround");
333 markObject.transform.parent = markGounds;
334 markObject.transform.localPosition = groundObject.transform.localPosition;
335 BoxCollider2D markCollider = markObject.AddComponent<BoxCollider2D>();
336 markCollider.size = new Vector2(0.1f, (height - 4)/100);
337 markCollider.offset = new Vector2(0, - height / 200);
338 }
339 }
340
341 private void createDeadLine()
342 {
343 deadLine = new GameObject(MapObjectNames.DeadLine);
344 deadLine.transform.localPosition = new Vector3(120, -3.4f, 0);
345 BoxCollider2D deadLineCollider = deadLine.AddComponent<BoxCollider2D>();
346 deadLineCollider.size = new Vector2(250, 0.5f);
347 deadLineCollider.isTrigger = true;
348
349 GameObject hellObject = new GameObject(MapObjectNames.HellObject);
350 hellObject.transform.localPosition = new Vector3(120, -6, 0);
351 BoxCollider2D hellCollider = hellObject.AddComponent<BoxCollider2D>();
352 hellCollider.size = new Vector2(250, 0.5f);
353 }
354
355 private void createFinishObject(Transform parentTransform)
356 {
357 for (int i = 0; i < parentTransform.childCount; i++)
358 {
359 GameObject finishObject = parentTransform.GetChild(i).gameObject;
360 finishObject.name = MapObjectNames.FinishObject;
361 if (finishObject.GetComponent<BoxCollider2D>() != null)
362 Destroy(finishObject.GetComponent<BoxCollider2D>());
363 BoxCollider2D finishCollider = finishObject.AddComponent<BoxCollider2D>();
364 finishCollider.size = new Vector2(20, 1000);
365 finishCollider.isTrigger = true;
366 }
367 }
368
369 public void Update()
370 {
371 if (isRunning)
372 {
373 m_time += Time.deltaTime;
374
375 taskbar.setParams(m_gold, m_score, m_time);
376 }
377 }
378
379 public void LateUpdate()
380 {
381 if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space))
382 {
383 if (InputController.Name == InputNames.GAMESCREEN)
384 {
385 if (InputController.IsScreen)
386 {
387 if(Time.timeScale != 0)
388 animals.getAnimal(0).GetComponent<Animal>().Jump();
389 //createBuiTien(-1.2f, 1f);
390 }
391 }
392 }
393 else if (Input.GetMouseButtonUp(0))
394 {
395 InputController.IsScreen = true;
396 }
397 if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Backspace))
398 {
399 if (!isPrepare)
400 {
401 if (dialog != null && dialog.activeSelf && InputController.Name == InputNames.DIALOG)
402 {
403 dialog.GetComponent<DialogUnity>().hideDialog();
404 pauseLayer.SetActive(true);
405 }else if (InputController.Name == InputNames.GAMESCREEN)
406 {
407 pauseGame();
408 dialog.GetComponent<DialogUnity>().showDialog();
409 pauseLayer.SetActive(false);
410 }
411 else if (pauseLayer.activeSelf)
412 {
413 resumeGame();
414 }
415 }
416 }
417 if (isPrepare)
418 {
419 isPrepare = false;
420
421 buttonSkills.setFonts(shopFont);
422 prepareGame();
423 }
424 }
425
426 private void createDialog()
427 {
428 dialog = (GameObject)Instantiate(dialog);
429 Vector3 p = dialog.transform.localPosition;
430 dialog.transform.localPosition = new Vector3(p.x, p.y, -8);
431 DialogUnity dialogUnity = dialog.GetComponent<DialogUnity>();
432 dialogUnity.setText("Do you want to quit?", "");
433 dialogUnity.setDialogTwo(
434 delegate()
435 {
436 SoundManager.LoadBgMusic("Sounds/menu", true);
437 Application.LoadLevel("MapScreen");
438 },
439 delegate()
440 {
441 pauseLayer.SetActive(true);
442 });
443 }
444
445 public void addDurtGold(Vector3 coinPosition)
446 {
447 GameObject durtGold = (GameObject)Instantiate(Resources.Load<GameObject>("Gold"));
448 durtGold.transform.localPosition = new Vector3(coinPosition.x, coinPosition.y, 0);
449 durtGold.transform.parent = goldLayer.transform;
450 durtGold.GetComponent<SpriteRenderer>().sortingLayerName = "MapObject";
451 }
452
453 public void showRevivalTask(float duration, EmotionType emotionType)
454 {
455 revivalTask.setIcon(emotionType);
456 revivalTask.show(duration);
457 setCombo(-1);
458 }
459
460 public void addScore(int score, Vector3 position)
461 {
462 if (score == 5)
463 {
464 GameObject addScore = new GameObject("Score");
465 addScore.transform.parent = scoreLayer.transform;
466 addScore.transform.localPosition = new Vector3(position.x, position.y, -1);
467 addScore.AddComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("Textures/add5");
468 addScore.layer = LayerMask.NameToLayer("Map");
469 addScore.GetComponent<SpriteRenderer>().sortingLayerName = "MapObject";
470 addScore.AddComponent<Actor>().addAction(new ActionMoveTo(position.x + 2, position.y + 1.5f, 1.5f));
471 Destroy(addScore, 1.5f);
472 }
473 else if (score == 10)
474 {
475 GameObject addScore = new GameObject("Score");
476 addScore.transform.parent = scoreLayer.transform;
477 addScore.transform.localPosition = new Vector3(position.x, position.y, -1);
478 addScore.AddComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("Textures/add10");
479 addScore.layer = LayerMask.NameToLayer("Map");
480 addScore.GetComponent<SpriteRenderer>().sortingLayerName = "MapObject";
481 addScore.AddComponent<Actor>().addAction(new ActionSequence(
482 new ActionMoveTo(position.x + 2, position.y + 1.5f, 1.5f),
483 new ActionRunnable(delegate() {
484 Destroy(addScore);
485 })
486 ));
487 // Destroy(addScore, 1.5f);
488 }
489 m_score += score;
490 }
491
492 public void eatSkillRandom(SkillType type, Vector3 position, int animalIndex)
493 {
494 if (animalIndex == 0)
495 {
496 if (useSkills)
497 {
498 GameObject skillRand = new GameObject("SkilRand");
499 skillRand.transform.parent = scoreLayer.transform;
500 skillRand.transform.localPosition = new Vector3(position.x, position.y, -1);
501 skillRand.AddComponent<SpriteRenderer>().sprite = buttonSkills.skillSprites[SkillRandom.getSkillIndex(type)];
502 skillRand.layer = LayerMask.NameToLayer("Map");
503 skillRand.GetComponent<SpriteRenderer>().sortingLayerName = "MapObject";
504 skillRand.AddComponent<Actor>().addAction(new ActionSequence(
505 new ActionMoveTo(position.x + 3, position.y + 1.5f, 1.5f),
506 new ActionRunnable(delegate() {
507 Destroy(skillRand);
508 })
509 ));
510 skillRand.transform.localScale = new Vector3(0.5f, 0.5f, skillRand.transform.localScale.z);
511 //Destroy(skillRand, 1.5f);
512
513 buttonSkills.eateSkill(type);
514
515 addScore(5, position);
516 }
517 }
518 else
519 {
520 int ra = Random.Range(1, 100);
521 if(ra <= perSkills[Attr.currentLevel])
522 addSkill(type, animalIndex);
523 }
524 }
525
526 public void pauseGame()
527 {
528 animals.setRunning(false);
529 background.setRuning(false);
530 InputController.Name = InputNames.DIALOG;
531 pauseLayer.SetActive(true);
532 isRunning = false;
533 revivalTask.setRunning(false);
534 setAnimationRunning(false);
535 setBulletRunning(false);
536 SoundManager.PauseMusic();
537 }
538
539 public void resumeGame()
540 {
541 InputController.Name = InputNames.GAMESCREEN;
542 animals.setRunning(true);
543 pauseLayer.SetActive(false);
544 background.setRuning(true);
545 isRunning = true;
546 revivalTask.setRunning(true);
547 setAnimationRunning(true);
548 setBulletRunning(true);
549 SoundManager.ResumeMusic("Sounds/bg1");
550 }
551
552 public void finishGame()
553 {
554 ARController.showInterstitialAd();
555 prepareGame();
556 //show result
557 resultLayer = (GameObject)Instantiate(resultLayer);
558 resultLayer.GetComponent<ResultLayer>().setParams(m_score, taskbar.getRankPlayer(), m_gold, m_time);
559
560 InputController.Name = InputNames.DIALOG;
561 SoundManager.stopMusic();
562 if (resultLayer.GetComponent<ResultLayer>().getStar() > 0)
563 {
564 SoundManager.playSoundLong("Sounds/votay");
565 }
566 else
567 {
568 SoundManager.playSoundLong("Sounds/thua");
569 }
570
571 }
572
573 private void setAnimationRunning(bool isRunning)
574 {
575 for (int i = 0; i < coinLayer.transform.childCount; i++)
576 {
577 coinLayer.transform.GetChild(i).gameObject.GetComponent<Bapk.Animation>().setRunning(isRunning);
578 }
579 for (int i = 0; i < goldLayer.transform.childCount; i++)
580 {
581 goldLayer.transform.GetChild(i).gameObject.GetComponent<Bapk.Animation>().setRunning(isRunning);
582 goldLayer.transform.GetChild(i).gameObject.GetComponent<Gold>().setRunning(isRunning);
583 }
584 for (int i = 0; i < boosterLayer.transform.childCount; i++)
585 {
586 boosterLayer.transform.GetChild(i).gameObject.GetComponent<Bapk.Animation>().setRunning(isRunning);
587 }
588 for (int i = 0; i < shadowLayer.transform.childCount; i++)
589 {
590 if (shadowLayer.transform.GetChild(i).gameObject.GetComponent<Actor>() != null)
591 shadowLayer.transform.GetChild(i).gameObject.GetComponent<Actor>().setRunning(isRunning);
592 }
593 }
594
595 private void setBulletRunning(bool isRunning)
596 {
597 for (int i = 0; i < bulletLayer.transform.childCount; i++)
598 {
599 GameObject bulletObject = bulletLayer.transform.GetChild(i).gameObject;
600 bulletObject.GetComponent<Bullet>().setRunning(isRunning);
601 if (bulletObject.GetComponent<Bapk.Animation>() != null)
602 bulletObject.GetComponent<Bapk.Animation>().setRunning(isRunning);
603 }
604
605 for (int i = 0; i < effectLayer.transform.childCount; i++)
606 {
607 GameObject effectObject = effectLayer.transform.GetChild(i).gameObject;
608 effectObject.GetComponent<Effect>().setRunning(isRunning);
609 }
610
611 for (int i = 0; i < scoreLayer.transform.childCount; i++)
612 {
613 GameObject scoreObject = scoreLayer.transform.GetChild(i).gameObject;
614 if (scoreObject.GetComponent<Actor>() != null)
615 scoreObject.GetComponent<Actor>().setRunning(isRunning);
616 }
617 }
618
619 public void guideGame()
620 {
621 guideLayer = (GameObject)Instantiate(Resources.Load<GameObject>("GuideLayer"));
622 guideLayer.GetComponent<GuideLayer>().gameScreen = this;
623 }
624
625 private void prepareGame()
626 {
627 animals.setRunning(false);
628 background.setRuning(false);
629 InputController.Name = InputNames.DIALOG;
630 isRunning = false;
631 revivalTask.setRunning(false);
632 setAnimationRunning(false);
633 }
634
635 public void OnApplicationPause(bool pauseStatus)
636 {
637 if (!pauseStatus)
638 {
639 if (InputController.Name != InputNames.DIALOG && InputController.Name != "")
640 {
641 pauseGame();
642 Debug.Log("Pause");
643 }
644 }
645 else
646 {
647 Debug.Log("Pause true");
648 }
649 }
650
651 public void showCounter()
652 {
653 if (counterLayer != null)
654 counterLayer.SetActive(true);
655 else
656 pauseGame();
657 }
658
659 public void addSkill(SkillType type, int animalIndex)
660 {
661 switch (type)
662 {
663 case SkillType.LUA:
664 {
665 GameObject bulletObject = (GameObject)Instantiate(Resources.Load<GameObject>("Bullets/LUA"));
666 bulletObject.name = "Bullet";
667 bulletObject.layer = LayerMask.NameToLayer("Animal" + (animalIndex + 1));
668 bulletObject.transform.parent = bulletLayer.transform;
669 GameObject animal = animals.getAnimal(animalIndex);
670 bulletObject.transform.localPosition = new Vector3(animal.transform.localPosition.x, animal.transform.localPosition.y + 0.2f, bulletObject.transform.localPosition.z);
671 Bullet bullet = bulletObject.GetComponent<Bullet>();
672 bullet.animalIndex = animalIndex;
673 }
674 break;
675 case SkillType.DONGBANG:
676 {
677 GameObject bulletObject = (GameObject)Instantiate(Resources.Load<GameObject>("Bullets/DONGBANG"));
678 bulletObject.name = "Bullet";
679 bulletObject.layer = LayerMask.NameToLayer("Animal" + (animalIndex + 1));
680 bulletObject.transform.parent = bulletLayer.transform;
681 GameObject animal = animals.getAnimal(animalIndex);
682 bulletObject.transform.localPosition = new Vector3(animal.transform.localPosition.x, animal.transform.localPosition.y + 0.2f, bulletObject.transform.localPosition.z);
683 Bullet bullet = bulletObject.GetComponent<Bullet>();
684 bullet.animalIndex = animalIndex;
685 }
686 break;
687 case SkillType.THIENTHACH:
688 {
689 for (int i = 0; i < 3; i++)
690 {
691 GameObject bulletObject = (GameObject)Instantiate(Resources.Load<GameObject>("Bullets/THIENTHACH"));
692 bulletObject.name = "Bullet";
693 bulletObject.layer = LayerMask.NameToLayer("Animal" + (animalIndex + 1));
694 bulletObject.transform.parent = bulletLayer.transform;
695 GameObject animal = animals.getAnimal(animalIndex);
696 bulletObject.transform.localPosition = new Vector3(animal.transform.localPosition.x + 0.2f, animal.transform.localPosition.y + 0.4f, bulletObject.transform.localPosition.z);
697 Bullet bullet = bulletObject.GetComponent<Bullet>();
698 bullet.animalIndex = animalIndex;
699 }
700 }
701 break;
702 case SkillType.SET:
703 {
704 GameObject bulletObject = (GameObject)Instantiate(Resources.Load<GameObject>("Bullets/SET"));
705 bulletObject.name = "Bullet";
706 bulletObject.layer = LayerMask.NameToLayer("Animal" + (animalIndex + 1));
707 bulletObject.transform.parent = bulletLayer.transform;
708 GameObject animal = animals.getAnimal(animalIndex);
709 bulletObject.transform.localPosition = new Vector3(animal.transform.localPosition.x + 0.2f, animal.transform.localPosition.y + 0.2f, bulletObject.transform.localPosition.z);
710 Bullet bullet = bulletObject.GetComponent<Bullet>();
711 bullet.animalIndex = animalIndex;
712 }
713 break;
714 case SkillType.BOM:
715 {
716 GameObject bulletObject = (GameObject)Instantiate(Resources.Load<GameObject>("Bullets/BOM"));
717 bulletObject.name = "Bullet";
718 bulletObject.layer = LayerMask.NameToLayer("Animal" + (animalIndex + 1));
719 bulletObject.transform.parent = bulletLayer.transform;
720 GameObject animal = animals.getAnimal(animalIndex);
721 bulletObject.transform.localPosition = new Vector3(animal.transform.localPosition.x , animal.transform.localPosition.y + 0.5f , bulletObject.transform.localPosition.z);
722 Bullet bullet = bulletObject.GetComponent<Bullet>();
723 bullet.animalIndex = animalIndex;
724 }
725 break;
726 case SkillType.DOICHO:
727 {
728 GameObject bulletObject = (GameObject)Instantiate(Resources.Load<GameObject>("Bullets/DOICHO"));
729 bulletObject.name = "Bullet";
730 bulletObject.layer = LayerMask.NameToLayer("Animal" + (animalIndex + 1));
731 bulletObject.transform.parent = bulletLayer.transform;
732 GameObject animal = animals.getAnimal(animalIndex);
733 bulletObject.transform.localPosition = new Vector3(animal.transform.localPosition.x + 0.3f, animal.transform.localPosition.y + 0.2f, bulletObject.transform.localPosition.z);
734 Bullet bullet = bulletObject.GetComponent<Bullet>();
735 bullet.animalIndex = animalIndex;
736 }
737 break;
738 case SkillType.BAOVE:
739 {
740 Animal animal = animals.getAnimal(animalIndex).GetComponent<Animal>();
741 if (!animal.IsProtected)
742 {
743 GameObject effectObject = (GameObject)Instantiate(Resources.Load<GameObject>("Effects/BAOVE"));
744 effectObject.name = "Effect";
745 effectObject.transform.parent = effectLayer.transform;
746 effectObject.GetComponent<Effect>().mapObjectTransform = animal.gameObject.transform;
747 animal.protectedEffect = effectObject.GetComponent<Effect>();
748 }
749 else
750 {
751 animal.protectedEffect.stateTime = 5;
752 }
753 animals.getAnimal(animalIndex).GetComponent<Animal>().setProtected();
754 }
755 break;
756 case SkillType.TANGTOC:
757 {
758 GameObject effectObject = (GameObject)Instantiate(Resources.Load<GameObject>("Effects/TANGTOC"));
759 effectObject.name = "Effect";
760 effectObject.transform.parent = effectLayer.transform;
761 effectObject.GetComponent<Effect>().mapObjectTransform = animals.getAnimal(animalIndex).transform;
762 animals.getAnimal(animalIndex).GetComponent<Animal>().setSpeedUp();
763 }
764 break;
765 }
766 }
767
768 public void shot(SkillType type)
769 {
770 buttonSkills.shot(type);
771 }
772
773 public void setCombo(int type, SkillType skillType)
774 {
775
776 int skillIndex = SkillRandom.getSkillIndex(skillType);
777 if (type == 1)
778 {
779 combo1.setCombo(skillIndex);
780 if (combo1.getNumberCombo() >= 2)
781 {
782 goldAddition.addGold1(20);
783 m_gold += combo1.getNumberCombo() * 5;
784 }
785 }
786 }
787
788 public void setCombo(int itemIndex)
789 {
790 if (itemIndex == -1)
791 {
792 combo1.setCombo(-1);
793 combo2.setCombo(-1);
794 return;
795 }
796 combo2.setCombo(itemIndex);
797 if (combo2.getNumberCombo() >= 2)
798 {
799 goldAddition.addGold2(20);
800 m_gold += combo1.getNumberCombo() * 5;
801 }
802 }
803
804 public GameObject getAnimal(int animalIndex)
805 {
806 return animals.getAnimal(animalIndex);
807 }
808
809 public void AnimalcollisionWithBullet(GameObject animalObject, GameObject bulletObject)
810 {
811 if (animalObject.GetComponent<Animal>().isStanding) return;
812 SkillType type = bulletObject.GetComponent<Bullet>().skillType;
813 int animalIndex = animalObject.GetComponent<Animal>().animalIndex;
814 switch (type)
815 {
816 case SkillType.LUA:
817 {
818 GameObject effectObject = (GameObject)Instantiate(Resources.Load<GameObject>("Effects/LUA"));
819 effectObject.transform.parent = effectLayer.transform;
820 effectObject.GetComponent<Effect>().mapObjectTransform = animalObject.transform;
821 Destroy(bulletObject);
822 animalObject.GetComponent<Animal>().setStand(true, 2, Animal.STATE_FIRE);
823 animalObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, -300));
824 if (animalIndex == 0)
825 {
826 showRevivalTask(2, EmotionType.CUOIDEU);
827 }
828 }
829 break;
830 case SkillType.DONGBANG:
831 {
832 GameObject effectObject = (GameObject)Instantiate(Resources.Load<GameObject>("Effects/DONGBANG"));
833 effectObject.transform.parent = effectLayer.transform;
834 effectObject.GetComponent<Effect>().mapObjectTransform = animalObject.transform;
835 Destroy(bulletObject);
836 animalObject.GetComponent<Animal>().setStand(true,2, Animal.STATE_RUN);
837 animalObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, -300));
838 if (animalIndex == 0)
839 {
840 showRevivalTask(2, EmotionType.HOAMAT);
841 }
842 }
843 break;
844 case SkillType.THIENTHACH:
845 {
846 {
847 GameObject effectObject = (GameObject)Instantiate(Resources.Load<GameObject>("Effects/THIENTHACH"));
848 effectObject.transform.parent = effectLayer.transform;
849 effectObject.GetComponent<Effect>().mapObjectTransform = animalObject.transform;
850 }
851 {
852 GameObject effectObject = (GameObject)Instantiate(Resources.Load<GameObject>("Effects/CHOANG"));
853 effectObject.transform.parent = effectLayer.transform;
854 effectObject.GetComponent<Effect>().mapObjectTransform = animalObject.transform;
855 }
856 Destroy(bulletObject);
857 animalObject.GetComponent<Animal>().setStand(true, 2, Animal.STATE_SHOCK);
858 animalObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, -300));
859 if (animalIndex == 0)
860 {
861 showRevivalTask(2, EmotionType.HOAMAT);
862 }
863 }
864 break;
865 case SkillType.SET:
866 {
867 GameObject effectObject = (GameObject)Instantiate(Resources.Load<GameObject>("Effects/SET"));
868 effectObject.transform.parent = effectLayer.transform;
869 effectObject.GetComponent<Effect>().mapObjectTransform = animalObject.transform;
870 animalObject.GetComponent<Animal>().setStand(true, 2, Animal.STATE_FIRE);
871 animalObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, -300));
872 if (animalIndex == 0)
873 {
874 showRevivalTask(2, EmotionType.HOAMAT);
875 }
876 }
877 break;
878 case SkillType.BOM:
879 {
880 GameObject effectObject = (GameObject)Instantiate(Resources.Load<GameObject>("Effects/LUA"));
881 effectObject.transform.parent = effectLayer.transform;
882 effectObject.GetComponent<Effect>().mapObjectTransform = animalObject.transform;
883 Destroy(bulletObject);
884 animalObject.GetComponent<Animal>().setStand(true, 2, Animal.STATE_FIRE);
885 animalObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, -300));
886 }
887 break;
888 case SkillType.DOICHO:
889 {
890 GameObject effectObject = (GameObject)Instantiate(Resources.Load<GameObject>("Effects/KHOI"));
891 effectObject.transform.parent = effectLayer.transform;
892 effectObject.GetComponent<Effect>().mapObjectTransform = animalObject.transform;
893 int animalIndexb = bulletObject.GetComponent<Bullet>().animalIndex;
894 Destroy(bulletObject);
895 GameObject animal2 = animals.getAnimal(animalIndexb);
896 GameObject effectObject2 = (GameObject)Instantiate(Resources.Load<GameObject>("Effects/KHOI"));
897 effectObject.transform.parent = effectLayer.transform;
898 effectObject2.GetComponent<Effect>().mapObjectTransform = animal2.transform;
899 Vector3 p1 = animalObject.transform.localPosition;
900 Vector3 p2 = animal2.transform.localPosition;
901 animal2.transform.localPosition = new Vector3(p1.x, p1.y, p2.z);
902 animalObject.transform.localPosition = new Vector3(p2.x, p2.y, p1.z);
903 }
904 break;
905 }
906 }
907 }
908 }
2 using System.Collections.Generic;
3 using Dialog;
4
5 namespace GamePlay
6 {
7 public class GameScreen : MonoBehaviour
8 {
9 private GameObject mapObject;
10 public Animals animals;
11 public GameObject goldLayer;
12 public GameObject coinLayer;
13 public GameObject boosterLayer;
14 public GameObject springsLayer;
15 public GameObject skillRandomLayer;
16 public GameObject shadowLayer;
17 public TaskBar taskbar;
18 public ButtonSkills buttonSkills;
19
20 public GameObject pauseLayer;
21
22 public Background background;
23
24 public GameObject bulletLayer;
25
26 public GameObject effectLayer;
27
28 public GameObject scoreLayer;
29
30 private GameObject deadLine;
31
32 public RevivalTask revivalTask;
33
34 private List<Vector3> revivalPositions;
35
36 public float m_time { get; set; }
37 public int m_gold { get; set; }
38 public int m_score { get; set; }
39
40 private bool isRunning;
41 private bool isPrepare;
42
43 private GameObject guideLayer;
44 public GameObject counterLayer;
45
46 public GameObject resultLayer;
47
48 private int[] skills;
49 private bool useSkills;
50
51 private BitmapFont shopFont;
52
53 private int[] perSkills;
54
55 public Combo combo1;
56 public Combo combo2;
57
58 public GameObject dialog;
59
60 public GoldAddition goldAddition;
61
62 public void Start()
63 {
64 InputController.Name = InputNames.DIALOG;
65
66 string[] mapNames = new string[] {"jungle","southpole","desert","volcano"};
67 int[] lvs = new int[] { 1, 2, 3, 2, 3, 4, 3, 4, 5, 1, 2, 4, 3, 4, 5 };
68
69 Debug.Log("WorldIndex : " + Attr.currentWorld + ", LevelIndex : " + Attr.currentLevel);
70
71 mapObject = (GameObject)Instantiate(Resources.Load("Maps/" + mapNames[Attr.currentWorld] + lvs[Attr.currentLevel]), new Vector3(-4, 2.5f, 0), Quaternion.identity);
72 mapObject.transform.localScale = new Vector3(0.01f, 0.01f, mapObject.transform.localScale.z);
73
74 revivalPositions = new List<Vector3>();
75
76 getSkillChoosed();
77
78 setupMap(mapObject);
79
80 int mapLayer = LayerMask.NameToLayer("Map");
81 mapObject.layer = mapLayer;
82
83 Transform[] transforms = mapObject.GetComponentsInChildren<Transform>(true);
84 for (int i = 0; i < transforms.Length; i++)
85 {
86 transforms[i].gameObject.layer = mapLayer;
87 }
88
89 shopFont = new BitmapFont("Fonts/shop_font", "Fonts/shop_font_xml", null);
90
91 BitmapFont comboFont = new BitmapFont("Fonts/font_combo", "Fonts/font_combo_xml", null);
92 combo1.setFont(comboFont);
93 combo2.setFont(comboFont);
94
95 createDialog();
96
97 m_gold = 0;
98 m_time = 0;
99 m_score = 0;
100
101 perSkills = new int[] { 30, 30, 35, 35, 40, 40, 45, 45, 50, 55, 60, 60, 65, 65, 70 };
102
103 isRunning = true;
104 //prepareGame();//khong goi o day vi co the chua chay het cac ham Start
105 isPrepare = true;
106 }
107
108 private void getSkillChoosed()
109 {
110 List<int> sks = new List<int>();
111 for (int i = 0; i < 3; i++)
112 {
113 if (Attr.currentSkills[i] != -1)
114 {
115 sks.Add(Attr.currentSkills[i]);
116 }
117 }
118 if (sks.Count > 0)//neu co chon skill
119 {
120 useSkills = true;
121 skills = new int[sks.Count];
122 for (int i = 0; i < sks.Count; i++)
123 {
124 skills[i] = sks[i];
125 }
126 }
127 else//neu khong chon skill thi lay tu cac skill unlock
128 {
129 int[] unlockStars = new int[] { 0, 0, 35, 135, 15, 55, 75, 95 };
130
131 int totalStar = 0;
132 for (int i = 0; i < 60; i++)
133 totalStar += Data.getData(Data.KEY_STAR + i);
134
135 for (int i = 0; i < 8; i++)
136 {
137 if (totalStar >= unlockStars[i])
138 {
139 sks.Add(i);
140 }
141 }
142 skills = new int[sks.Count];
143 for (int i = 0; i < sks.Count; i++)
144 {
145 skills[i] = sks[i];
146 }
147 }
148 }
149
150 private void setupMap(GameObject mObject)
151 {
152 GameObject markGounds = new GameObject("MarkGrounds");
153 markGounds.transform.parent = mObject.transform;
154 markGounds.transform.localScale = new Vector3(1, 1, 1);
155 markGounds.transform.localPosition = new Vector3(0, 0, 0);
156
157 for (int i = 0; i < mObject.transform.childCount; i++)
158 {
159 Transform childTransform = mObject.transform.GetChild(i);
160 string childname = childTransform.gameObject.name;
161 if (childname == MapObjectNames.MarkObject)
162 {
163 createMarkObjects(childTransform);
164 }
165 else if (childname == MapObjectNames.BoostObject)
166 {
167 createBoosterObjects(childTransform);
168 }
169 else if (childname == MapObjectNames.SpringsObject)
170 {
171 createSpringsObjects(childTransform);
172 }
173 else if (childname == MapObjectNames.HoleObject)
174 {
175 createHoles(childTransform);
176 }
177 else if (childname == MapObjectNames.Coin)
178 {
179 createCoins(childTransform);
180 }
181 else if (childname == MapObjectNames.SkillObject)
182 {
183 createSkillRandoms(childTransform);
184 }
185 else if (childname == MapObjectNames.RevivalObject)
186 {
187 createRevivalObjects(childTransform);
188 }
189 else if (childname == MapObjectNames.GroundObject)
190 {
191 createGrounds(childTransform, markGounds.transform);
192 }
193 else if (childname == MapObjectNames.FinishObject)
194 {
195 createFinishObject(childTransform);
196 }
197 }
198
199 createDeadLine();
200 }
201
202 private void createMarkObjects(Transform parentTransform)
203 {
204 for (int i = 0; i < parentTransform.childCount; i++)
205 {
206 GameObject markObject = parentTransform.GetChild(i).gameObject;
207 markObject.name = MapObjectNames.MarkObject;
208 if (markObject.GetComponent<BoxCollider2D>() != null)
209 Destroy(markObject.GetComponent<BoxCollider2D>());
210 BoxCollider2D childCollider = markObject.AddComponent<BoxCollider2D>();
211 childCollider.size = new Vector2(20, 1000);
212 childCollider.isTrigger = true;
213 }
214 }
215
216 private void createBoosterObjects(Transform parentTransform)
217 {
218 for (int i = 0; i < parentTransform.childCount; i++)
219 {
220 GameObject boosterObject = (GameObject)Instantiate(Resources.Load<GameObject>("Booster"));
221 GameObject c = parentTransform.GetChild(i).gameObject;
222 boosterObject.transform.position = c.transform.position;
223 boosterObject.transform.localScale = new Vector3(1, 1, 0);
224 boosterObject.transform.parent = boosterLayer.transform;
225 BoxCollider2D childCollider = boosterObject.AddComponent<BoxCollider2D>();
226 childCollider.size = new Vector2(0.9f, 0.2f);
227 childCollider.isTrigger = true;
228 boosterObject.transform.position -= new Vector3(-0.45f, 0.1f, 0);
229 boosterObject.name = MapObjectNames.BoostObject;
230
231 Destroy(c);
232 }
233 }
234
235 private void createSpringsObjects(Transform parentTransform)
236 {
237 for (int i = 0; i < parentTransform.childCount; i++)
238 {
239 GameObject springsObject = (GameObject)Instantiate(Resources.Load<GameObject>("Springs"));
240 GameObject c = parentTransform.GetChild(i).gameObject;
241 springsObject.transform.position = c.transform.position;
242 springsObject.transform.parent = springsLayer.transform;
243 springsObject.transform.position -= new Vector3(-0.2f, 0.3f, 0);
244 springsObject.name = MapObjectNames.SpringsObject;
245 Destroy(c);
246 }
247 }
248
249 private void createHoles(Transform parentTransform)
250 {
251 for (int i = 0; i < parentTransform.childCount; i++)
252 {
253 BoxCollider2D childCollider = parentTransform.GetChild(i).gameObject.GetComponent<BoxCollider2D>();
254 childCollider.isTrigger = true;
255 parentTransform.GetChild(i).gameObject.name = MapObjectNames.HoleObject;
256 }
257 }
258
259 private void createCoins(Transform parentTransform)
260 {
261 for (int i = 0; i < parentTransform.childCount; i++)
262 {
263 GameObject coinObject = (GameObject)Instantiate(Resources.Load<GameObject>("Coin"));
264 coinObject.name = MapObjectNames.Coin;
265 GameObject c = parentTransform.GetChild(i).gameObject;
266 coinObject.transform.position = c.transform.position;
267 coinObject.transform.localScale = new Vector3(1, 1, 0);
268 coinObject.transform.parent = coinLayer.transform;
269 BoxCollider2D childCollider = coinObject.AddComponent<BoxCollider2D>();
270 childCollider.size = new Vector2(0.3f, 0.3f);
271 childCollider.isTrigger = true;
272
273 Destroy(c);
274 }
275 }
276
277 private void createSkillRandoms(Transform parentTransform)
278 {
279 for (int i = 0; i < parentTransform.childCount; i++)
280 {
281 GameObject skillObject = (GameObject)Instantiate(Resources.Load<GameObject>("SkillRandom"));
282 skillObject.name = MapObjectNames.SkillObject;
283 GameObject c = parentTransform.GetChild(i).gameObject;
284 skillObject.transform.position = c.transform.position;
285 skillObject.transform.parent = skillRandomLayer.transform;
286 skillObject.transform.position -= new Vector3(-0.2f, 0.2f, 0);
287
288 skillObject.AddComponent<SkillRandom>().skillType = SkillRandom.getSkillType(skills[Random.Range(0, skills.Length)]);
289 // Debug.Log(skillObject.GetComponent<SkillRandom>().skillType.ToString());
290
291 Destroy(c);
292 }
293 }
294
295 private void createRevivalObjects(Transform parentTransform)
296 {
297 for (int i = 0; i < parentTransform.childCount; i++)
298 {
299 GameObject revivalObject = parentTransform.GetChild(i).gameObject;
300 if (revivalObject.GetComponent<BoxCollider2D>() != null)
301 Destroy(revivalObject.GetComponent<BoxCollider2D>());
302 BoxCollider2D childCollider = revivalObject.AddComponent<BoxCollider2D>();
303 childCollider.size = new Vector2(20, 1000);
304 childCollider.isTrigger = true;
305 revivalObject.name = MapObjectNames.RevivalObject;
306 revivalPositions.Add(revivalObject.transform.localPosition);
307 revivalObject.AddComponent<RevivalObject>().revialIndex = i;
308 }
309 }
310
311 public Vector3 getRevivalPosition(int revivalIndex)
312 {
313 if(revivalIndex >= revivalPositions.Count)
314 return revivalPositions[revivalPositions.Count - 1];
315 return revivalPositions[revivalIndex];
316 }
317
318 private void createGrounds(Transform parentTransform, Transform markGounds)
319 {
320 for (int i = 0; i < parentTransform.childCount; i++)
321 {
322 GameObject groundObject = parentTransform.GetChild(i).gameObject;
323 groundObject.name = "GroundObject";
324 Rigidbody2D groundBody = groundObject.AddComponent<Rigidbody2D>();
325 groundBody.isKinematic = true;
326 groundBody.gravityScale = 0;
327
328 BoxCollider2D groundCollider = groundObject.GetComponent<BoxCollider2D>();
329
330 //them 1 cai gameobject chua boxcollider ngay truoc cac khoi dat
331 float height = groundObject.GetComponent<BoxCollider2D>().size.y;
332 GameObject markObject = new GameObject("MarkGround");
333 markObject.transform.parent = markGounds;
334 markObject.transform.localPosition = groundObject.transform.localPosition;
335 BoxCollider2D markCollider = markObject.AddComponent<BoxCollider2D>();
336 markCollider.size = new Vector2(0.1f, (height - 4)/100);
337 markCollider.offset = new Vector2(0, - height / 200);
338 }
339 }
340
341 private void createDeadLine()
342 {
343 deadLine = new GameObject(MapObjectNames.DeadLine);
344 deadLine.transform.localPosition = new Vector3(120, -3.4f, 0);
345 BoxCollider2D deadLineCollider = deadLine.AddComponent<BoxCollider2D>();
346 deadLineCollider.size = new Vector2(250, 0.5f);
347 deadLineCollider.isTrigger = true;
348
349 GameObject hellObject = new GameObject(MapObjectNames.HellObject);
350 hellObject.transform.localPosition = new Vector3(120, -6, 0);
351 BoxCollider2D hellCollider = hellObject.AddComponent<BoxCollider2D>();
352 hellCollider.size = new Vector2(250, 0.5f);
353 }
354
355 private void createFinishObject(Transform parentTransform)
356 {
357 for (int i = 0; i < parentTransform.childCount; i++)
358 {
359 GameObject finishObject = parentTransform.GetChild(i).gameObject;
360 finishObject.name = MapObjectNames.FinishObject;
361 if (finishObject.GetComponent<BoxCollider2D>() != null)
362 Destroy(finishObject.GetComponent<BoxCollider2D>());
363 BoxCollider2D finishCollider = finishObject.AddComponent<BoxCollider2D>();
364 finishCollider.size = new Vector2(20, 1000);
365 finishCollider.isTrigger = true;
366 }
367 }
368
369 public void Update()
370 {
371 if (isRunning)
372 {
373 m_time += Time.deltaTime;
374
375 taskbar.setParams(m_gold, m_score, m_time);
376 }
377 }
378
379 public void LateUpdate()
380 {
381 if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space))
382 {
383 if (InputController.Name == InputNames.GAMESCREEN)
384 {
385 if (InputController.IsScreen)
386 {
387 if(Time.timeScale != 0)
388 animals.getAnimal(0).GetComponent<Animal>().Jump();
389 //createBuiTien(-1.2f, 1f);
390 }
391 }
392 }
393 else if (Input.GetMouseButtonUp(0))
394 {
395 InputController.IsScreen = true;
396 }
397 if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Backspace))
398 {
399 if (!isPrepare)
400 {
401 if (dialog != null && dialog.activeSelf && InputController.Name == InputNames.DIALOG)
402 {
403 dialog.GetComponent<DialogUnity>().hideDialog();
404 pauseLayer.SetActive(true);
405 }else if (InputController.Name == InputNames.GAMESCREEN)
406 {
407 pauseGame();
408 dialog.GetComponent<DialogUnity>().showDialog();
409 pauseLayer.SetActive(false);
410 }
411 else if (pauseLayer.activeSelf)
412 {
413 resumeGame();
414 }
415 }
416 }
417 if (isPrepare)
418 {
419 isPrepare = false;
420
421 buttonSkills.setFonts(shopFont);
422 prepareGame();
423 }
424 }
425
426 private void createDialog()
427 {
428 dialog = (GameObject)Instantiate(dialog);
429 Vector3 p = dialog.transform.localPosition;
430 dialog.transform.localPosition = new Vector3(p.x, p.y, -8);
431 DialogUnity dialogUnity = dialog.GetComponent<DialogUnity>();
432 dialogUnity.setText("Do you want to quit?", "");
433 dialogUnity.setDialogTwo(
434 delegate()
435 {
436 SoundManager.LoadBgMusic("Sounds/menu", true);
437 Application.LoadLevel("MapScreen");
438 },
439 delegate()
440 {
441 pauseLayer.SetActive(true);
442 });
443 }
444
445 public void addDurtGold(Vector3 coinPosition)
446 {
447 GameObject durtGold = (GameObject)Instantiate(Resources.Load<GameObject>("Gold"));
448 durtGold.transform.localPosition = new Vector3(coinPosition.x, coinPosition.y, 0);
449 durtGold.transform.parent = goldLayer.transform;
450 durtGold.GetComponent<SpriteRenderer>().sortingLayerName = "MapObject";
451 }
452
453 public void showRevivalTask(float duration, EmotionType emotionType)
454 {
455 revivalTask.setIcon(emotionType);
456 revivalTask.show(duration);
457 setCombo(-1);
458 }
459
460 public void addScore(int score, Vector3 position)
461 {
462 if (score == 5)
463 {
464 GameObject addScore = new GameObject("Score");
465 addScore.transform.parent = scoreLayer.transform;
466 addScore.transform.localPosition = new Vector3(position.x, position.y, -1);
467 addScore.AddComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("Textures/add5");
468 addScore.layer = LayerMask.NameToLayer("Map");
469 addScore.GetComponent<SpriteRenderer>().sortingLayerName = "MapObject";
470 addScore.AddComponent<Actor>().addAction(new ActionMoveTo(position.x + 2, position.y + 1.5f, 1.5f));
471 Destroy(addScore, 1.5f);
472 }
473 else if (score == 10)
474 {
475 GameObject addScore = new GameObject("Score");
476 addScore.transform.parent = scoreLayer.transform;
477 addScore.transform.localPosition = new Vector3(position.x, position.y, -1);
478 addScore.AddComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("Textures/add10");
479 addScore.layer = LayerMask.NameToLayer("Map");
480 addScore.GetComponent<SpriteRenderer>().sortingLayerName = "MapObject";
481 addScore.AddComponent<Actor>().addAction(new ActionSequence(
482 new ActionMoveTo(position.x + 2, position.y + 1.5f, 1.5f),
483 new ActionRunnable(delegate() {
484 Destroy(addScore);
485 })
486 ));
487 // Destroy(addScore, 1.5f);
488 }
489 m_score += score;
490 }
491
492 public void eatSkillRandom(SkillType type, Vector3 position, int animalIndex)
493 {
494 if (animalIndex == 0)
495 {
496 if (useSkills)
497 {
498 GameObject skillRand = new GameObject("SkilRand");
499 skillRand.transform.parent = scoreLayer.transform;
500 skillRand.transform.localPosition = new Vector3(position.x, position.y, -1);
501 skillRand.AddComponent<SpriteRenderer>().sprite = buttonSkills.skillSprites[SkillRandom.getSkillIndex(type)];
502 skillRand.layer = LayerMask.NameToLayer("Map");
503 skillRand.GetComponent<SpriteRenderer>().sortingLayerName = "MapObject";
504 skillRand.AddComponent<Actor>().addAction(new ActionSequence(
505 new ActionMoveTo(position.x + 3, position.y + 1.5f, 1.5f),
506 new ActionRunnable(delegate() {
507 Destroy(skillRand);
508 })
509 ));
510 skillRand.transform.localScale = new Vector3(0.5f, 0.5f, skillRand.transform.localScale.z);
511 //Destroy(skillRand, 1.5f);
512
513 buttonSkills.eateSkill(type);
514
515 addScore(5, position);
516 }
517 }
518 else
519 {
520 int ra = Random.Range(1, 100);
521 if(ra <= perSkills[Attr.currentLevel])
522 addSkill(type, animalIndex);
523 }
524 }
525
526 public void pauseGame()
527 {
528 animals.setRunning(false);
529 background.setRuning(false);
530 InputController.Name = InputNames.DIALOG;
531 pauseLayer.SetActive(true);
532 isRunning = false;
533 revivalTask.setRunning(false);
534 setAnimationRunning(false);
535 setBulletRunning(false);
536 SoundManager.PauseMusic();
537 }
538
539 public void resumeGame()
540 {
541 InputController.Name = InputNames.GAMESCREEN;
542 animals.setRunning(true);
543 pauseLayer.SetActive(false);
544 background.setRuning(true);
545 isRunning = true;
546 revivalTask.setRunning(true);
547 setAnimationRunning(true);
548 setBulletRunning(true);
549 SoundManager.ResumeMusic("Sounds/bg1");
550 }
551
552 public void finishGame()
553 {
554 ARController.showInterstitialAd();
555 prepareGame();
556 //show result
557 resultLayer = (GameObject)Instantiate(resultLayer);
558 resultLayer.GetComponent<ResultLayer>().setParams(m_score, taskbar.getRankPlayer(), m_gold, m_time);
559
560 InputController.Name = InputNames.DIALOG;
561 SoundManager.stopMusic();
562 if (resultLayer.GetComponent<ResultLayer>().getStar() > 0)
563 {
564 SoundManager.playSoundLong("Sounds/votay");
565 }
566 else
567 {
568 SoundManager.playSoundLong("Sounds/thua");
569 }
570
571 }
572
573 private void setAnimationRunning(bool isRunning)
574 {
575 for (int i = 0; i < coinLayer.transform.childCount; i++)
576 {
577 coinLayer.transform.GetChild(i).gameObject.GetComponent<Bapk.Animation>().setRunning(isRunning);
578 }
579 for (int i = 0; i < goldLayer.transform.childCount; i++)
580 {
581 goldLayer.transform.GetChild(i).gameObject.GetComponent<Bapk.Animation>().setRunning(isRunning);
582 goldLayer.transform.GetChild(i).gameObject.GetComponent<Gold>().setRunning(isRunning);
583 }
584 for (int i = 0; i < boosterLayer.transform.childCount; i++)
585 {
586 boosterLayer.transform.GetChild(i).gameObject.GetComponent<Bapk.Animation>().setRunning(isRunning);
587 }
588 for (int i = 0; i < shadowLayer.transform.childCount; i++)
589 {
590 if (shadowLayer.transform.GetChild(i).gameObject.GetComponent<Actor>() != null)
591 shadowLayer.transform.GetChild(i).gameObject.GetComponent<Actor>().setRunning(isRunning);
592 }
593 }
594
595 private void setBulletRunning(bool isRunning)
596 {
597 for (int i = 0; i < bulletLayer.transform.childCount; i++)
598 {
599 GameObject bulletObject = bulletLayer.transform.GetChild(i).gameObject;
600 bulletObject.GetComponent<Bullet>().setRunning(isRunning);
601 if (bulletObject.GetComponent<Bapk.Animation>() != null)
602 bulletObject.GetComponent<Bapk.Animation>().setRunning(isRunning);
603 }
604
605 for (int i = 0; i < effectLayer.transform.childCount; i++)
606 {
607 GameObject effectObject = effectLayer.transform.GetChild(i).gameObject;
608 effectObject.GetComponent<Effect>().setRunning(isRunning);
609 }
610
611 for (int i = 0; i < scoreLayer.transform.childCount; i++)
612 {
613 GameObject scoreObject = scoreLayer.transform.GetChild(i).gameObject;
614 if (scoreObject.GetComponent<Actor>() != null)
615 scoreObject.GetComponent<Actor>().setRunning(isRunning);
616 }
617 }
618
619 public void guideGame()
620 {
621 guideLayer = (GameObject)Instantiate(Resources.Load<GameObject>("GuideLayer"));
622 guideLayer.GetComponent<GuideLayer>().gameScreen = this;
623 }
624
625 private void prepareGame()
626 {
627 animals.setRunning(false);
628 background.setRuning(false);
629 InputController.Name = InputNames.DIALOG;
630 isRunning = false;
631 revivalTask.setRunning(false);
632 setAnimationRunning(false);
633 }
634
635 public void OnApplicationPause(bool pauseStatus)
636 {
637 if (!pauseStatus)
638 {
639 if (InputController.Name != InputNames.DIALOG && InputController.Name != "")
640 {
641 pauseGame();
642 Debug.Log("Pause");
643 }
644 }
645 else
646 {
647 Debug.Log("Pause true");
648 }
649 }
650
651 public void showCounter()
652 {
653 if (counterLayer != null)
654 counterLayer.SetActive(true);
655 else
656 pauseGame();
657 }
658
659 public void addSkill(SkillType type, int animalIndex)
660 {
661 switch (type)
662 {
663 case SkillType.LUA:
664 {
665 GameObject bulletObject = (GameObject)Instantiate(Resources.Load<GameObject>("Bullets/LUA"));
666 bulletObject.name = "Bullet";
667 bulletObject.layer = LayerMask.NameToLayer("Animal" + (animalIndex + 1));
668 bulletObject.transform.parent = bulletLayer.transform;
669 GameObject animal = animals.getAnimal(animalIndex);
670 bulletObject.transform.localPosition = new Vector3(animal.transform.localPosition.x, animal.transform.localPosition.y + 0.2f, bulletObject.transform.localPosition.z);
671 Bullet bullet = bulletObject.GetComponent<Bullet>();
672 bullet.animalIndex = animalIndex;
673 }
674 break;
675 case SkillType.DONGBANG:
676 {
677 GameObject bulletObject = (GameObject)Instantiate(Resources.Load<GameObject>("Bullets/DONGBANG"));
678 bulletObject.name = "Bullet";
679 bulletObject.layer = LayerMask.NameToLayer("Animal" + (animalIndex + 1));
680 bulletObject.transform.parent = bulletLayer.transform;
681 GameObject animal = animals.getAnimal(animalIndex);
682 bulletObject.transform.localPosition = new Vector3(animal.transform.localPosition.x, animal.transform.localPosition.y + 0.2f, bulletObject.transform.localPosition.z);
683 Bullet bullet = bulletObject.GetComponent<Bullet>();
684 bullet.animalIndex = animalIndex;
685 }
686 break;
687 case SkillType.THIENTHACH:
688 {
689 for (int i = 0; i < 3; i++)
690 {
691 GameObject bulletObject = (GameObject)Instantiate(Resources.Load<GameObject>("Bullets/THIENTHACH"));
692 bulletObject.name = "Bullet";
693 bulletObject.layer = LayerMask.NameToLayer("Animal" + (animalIndex + 1));
694 bulletObject.transform.parent = bulletLayer.transform;
695 GameObject animal = animals.getAnimal(animalIndex);
696 bulletObject.transform.localPosition = new Vector3(animal.transform.localPosition.x + 0.2f, animal.transform.localPosition.y + 0.4f, bulletObject.transform.localPosition.z);
697 Bullet bullet = bulletObject.GetComponent<Bullet>();
698 bullet.animalIndex = animalIndex;
699 }
700 }
701 break;
702 case SkillType.SET:
703 {
704 GameObject bulletObject = (GameObject)Instantiate(Resources.Load<GameObject>("Bullets/SET"));
705 bulletObject.name = "Bullet";
706 bulletObject.layer = LayerMask.NameToLayer("Animal" + (animalIndex + 1));
707 bulletObject.transform.parent = bulletLayer.transform;
708 GameObject animal = animals.getAnimal(animalIndex);
709 bulletObject.transform.localPosition = new Vector3(animal.transform.localPosition.x + 0.2f, animal.transform.localPosition.y + 0.2f, bulletObject.transform.localPosition.z);
710 Bullet bullet = bulletObject.GetComponent<Bullet>();
711 bullet.animalIndex = animalIndex;
712 }
713 break;
714 case SkillType.BOM:
715 {
716 GameObject bulletObject = (GameObject)Instantiate(Resources.Load<GameObject>("Bullets/BOM"));
717 bulletObject.name = "Bullet";
718 bulletObject.layer = LayerMask.NameToLayer("Animal" + (animalIndex + 1));
719 bulletObject.transform.parent = bulletLayer.transform;
720 GameObject animal = animals.getAnimal(animalIndex);
721 bulletObject.transform.localPosition = new Vector3(animal.transform.localPosition.x , animal.transform.localPosition.y + 0.5f , bulletObject.transform.localPosition.z);
722 Bullet bullet = bulletObject.GetComponent<Bullet>();
723 bullet.animalIndex = animalIndex;
724 }
725 break;
726 case SkillType.DOICHO:
727 {
728 GameObject bulletObject = (GameObject)Instantiate(Resources.Load<GameObject>("Bullets/DOICHO"));
729 bulletObject.name = "Bullet";
730 bulletObject.layer = LayerMask.NameToLayer("Animal" + (animalIndex + 1));
731 bulletObject.transform.parent = bulletLayer.transform;
732 GameObject animal = animals.getAnimal(animalIndex);
733 bulletObject.transform.localPosition = new Vector3(animal.transform.localPosition.x + 0.3f, animal.transform.localPosition.y + 0.2f, bulletObject.transform.localPosition.z);
734 Bullet bullet = bulletObject.GetComponent<Bullet>();
735 bullet.animalIndex = animalIndex;
736 }
737 break;
738 case SkillType.BAOVE:
739 {
740 Animal animal = animals.getAnimal(animalIndex).GetComponent<Animal>();
741 if (!animal.IsProtected)
742 {
743 GameObject effectObject = (GameObject)Instantiate(Resources.Load<GameObject>("Effects/BAOVE"));
744 effectObject.name = "Effect";
745 effectObject.transform.parent = effectLayer.transform;
746 effectObject.GetComponent<Effect>().mapObjectTransform = animal.gameObject.transform;
747 animal.protectedEffect = effectObject.GetComponent<Effect>();
748 }
749 else
750 {
751 animal.protectedEffect.stateTime = 5;
752 }
753 animals.getAnimal(animalIndex).GetComponent<Animal>().setProtected();
754 }
755 break;
756 case SkillType.TANGTOC:
757 {
758 GameObject effectObject = (GameObject)Instantiate(Resources.Load<GameObject>("Effects/TANGTOC"));
759 effectObject.name = "Effect";
760 effectObject.transform.parent = effectLayer.transform;
761 effectObject.GetComponent<Effect>().mapObjectTransform = animals.getAnimal(animalIndex).transform;
762 animals.getAnimal(animalIndex).GetComponent<Animal>().setSpeedUp();
763 }
764 break;
765 }
766 }
767
768 public void shot(SkillType type)
769 {
770 buttonSkills.shot(type);
771 }
772
773 public void setCombo(int type, SkillType skillType)
774 {
775
776 int skillIndex = SkillRandom.getSkillIndex(skillType);
777 if (type == 1)
778 {
779 combo1.setCombo(skillIndex);
780 if (combo1.getNumberCombo() >= 2)
781 {
782 goldAddition.addGold1(20);
783 m_gold += combo1.getNumberCombo() * 5;
784 }
785 }
786 }
787
788 public void setCombo(int itemIndex)
789 {
790 if (itemIndex == -1)
791 {
792 combo1.setCombo(-1);
793 combo2.setCombo(-1);
794 return;
795 }
796 combo2.setCombo(itemIndex);
797 if (combo2.getNumberCombo() >= 2)
798 {
799 goldAddition.addGold2(20);
800 m_gold += combo1.getNumberCombo() * 5;
801 }
802 }
803
804 public GameObject getAnimal(int animalIndex)
805 {
806 return animals.getAnimal(animalIndex);
807 }
808
809 public void AnimalcollisionWithBullet(GameObject animalObject, GameObject bulletObject)
810 {
811 if (animalObject.GetComponent<Animal>().isStanding) return;
812 SkillType type = bulletObject.GetComponent<Bullet>().skillType;
813 int animalIndex = animalObject.GetComponent<Animal>().animalIndex;
814 switch (type)
815 {
816 case SkillType.LUA:
817 {
818 GameObject effectObject = (GameObject)Instantiate(Resources.Load<GameObject>("Effects/LUA"));
819 effectObject.transform.parent = effectLayer.transform;
820 effectObject.GetComponent<Effect>().mapObjectTransform = animalObject.transform;
821 Destroy(bulletObject);
822 animalObject.GetComponent<Animal>().setStand(true, 2, Animal.STATE_FIRE);
823 animalObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, -300));
824 if (animalIndex == 0)
825 {
826 showRevivalTask(2, EmotionType.CUOIDEU);
827 }
828 }
829 break;
830 case SkillType.DONGBANG:
831 {
832 GameObject effectObject = (GameObject)Instantiate(Resources.Load<GameObject>("Effects/DONGBANG"));
833 effectObject.transform.parent = effectLayer.transform;
834 effectObject.GetComponent<Effect>().mapObjectTransform = animalObject.transform;
835 Destroy(bulletObject);
836 animalObject.GetComponent<Animal>().setStand(true,2, Animal.STATE_RUN);
837 animalObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, -300));
838 if (animalIndex == 0)
839 {
840 showRevivalTask(2, EmotionType.HOAMAT);
841 }
842 }
843 break;
844 case SkillType.THIENTHACH:
845 {
846 {
847 GameObject effectObject = (GameObject)Instantiate(Resources.Load<GameObject>("Effects/THIENTHACH"));
848 effectObject.transform.parent = effectLayer.transform;
849 effectObject.GetComponent<Effect>().mapObjectTransform = animalObject.transform;
850 }
851 {
852 GameObject effectObject = (GameObject)Instantiate(Resources.Load<GameObject>("Effects/CHOANG"));
853 effectObject.transform.parent = effectLayer.transform;
854 effectObject.GetComponent<Effect>().mapObjectTransform = animalObject.transform;
855 }
856 Destroy(bulletObject);
857 animalObject.GetComponent<Animal>().setStand(true, 2, Animal.STATE_SHOCK);
858 animalObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, -300));
859 if (animalIndex == 0)
860 {
861 showRevivalTask(2, EmotionType.HOAMAT);
862 }
863 }
864 break;
865 case SkillType.SET:
866 {
867 GameObject effectObject = (GameObject)Instantiate(Resources.Load<GameObject>("Effects/SET"));
868 effectObject.transform.parent = effectLayer.transform;
869 effectObject.GetComponent<Effect>().mapObjectTransform = animalObject.transform;
870 animalObject.GetComponent<Animal>().setStand(true, 2, Animal.STATE_FIRE);
871 animalObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, -300));
872 if (animalIndex == 0)
873 {
874 showRevivalTask(2, EmotionType.HOAMAT);
875 }
876 }
877 break;
878 case SkillType.BOM:
879 {
880 GameObject effectObject = (GameObject)Instantiate(Resources.Load<GameObject>("Effects/LUA"));
881 effectObject.transform.parent = effectLayer.transform;
882 effectObject.GetComponent<Effect>().mapObjectTransform = animalObject.transform;
883 Destroy(bulletObject);
884 animalObject.GetComponent<Animal>().setStand(true, 2, Animal.STATE_FIRE);
885 animalObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, -300));
886 }
887 break;
888 case SkillType.DOICHO:
889 {
890 GameObject effectObject = (GameObject)Instantiate(Resources.Load<GameObject>("Effects/KHOI"));
891 effectObject.transform.parent = effectLayer.transform;
892 effectObject.GetComponent<Effect>().mapObjectTransform = animalObject.transform;
893 int animalIndexb = bulletObject.GetComponent<Bullet>().animalIndex;
894 Destroy(bulletObject);
895 GameObject animal2 = animals.getAnimal(animalIndexb);
896 GameObject effectObject2 = (GameObject)Instantiate(Resources.Load<GameObject>("Effects/KHOI"));
897 effectObject.transform.parent = effectLayer.transform;
898 effectObject2.GetComponent<Effect>().mapObjectTransform = animal2.transform;
899 Vector3 p1 = animalObject.transform.localPosition;
900 Vector3 p2 = animal2.transform.localPosition;
901 animal2.transform.localPosition = new Vector3(p1.x, p1.y, p2.z);
902 animalObject.transform.localPosition = new Vector3(p2.x, p2.y, p1.z);
903 }
904 break;
905 }
906 }
907 }
908 }
prepareGame();khong goi o day vi co the chua chay het cac ham Start
if (sks.Count > 0)neu co chon skill
elseneu khong chon skill thi lay tu cac skill unlock
Debug.Log(skillObject.GetComponent
them 1 cai gameobject chua boxcollider ngay truoc cac khoi dat
createBuiTien(-1.2f, 1f);
Destroy(addScore, 1.5f);
Destroy(skillRand, 1.5f);
show result